package com.waterworld.watch.home.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.waterworld.watch.R;
import com.waterworld.watch.chat.activity.SelectWatchActivity;
import com.waterworld.watch.chat.event.HasReadAllMsgEvent;
import com.waterworld.watch.chat.event.NotReadMsgEvent;
import com.waterworld.watch.common.bean.BindWatchBean;
import com.waterworld.watch.common.bean.ImeiBean;
import com.waterworld.watch.common.bean.IntimateSetBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.bean.WatchUserInfoBean;
import com.waterworld.watch.common.customview.CustomDialog;
import com.waterworld.watch.common.customview.CustomViewPager;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.PhotoUtils;
import com.waterworld.watch.home.adapter.HomePagerAdapter;
import com.waterworld.watch.home.adapter.BabyAdapter;
import com.waterworld.watch.common.customview.CommonPopupWindow;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.event.UpdateWatchInfoEvent;
import com.waterworld.watch.home.event.UserInfoEvent;
import com.waterworld.watch.home.event.WatchUserInfoEvent;
import com.waterworld.watch.home.fragment.HomeFragment;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;
import com.waterworld.watch.login.event.AutoLoginEvent;
import com.waterworld.watch.mine.fragment.MineFragment;
import com.waterworld.watch.chat.fragment.ChatFragment;
import com.waterworld.watch.location.fragment.LocationFragment;
import com.waterworld.watch.mine.interfaces.IMineManager;
import com.waterworld.watch.mine.manager.MineManager;
import com.waterworld.watch.push.contant.MessageContants;
import com.waterworld.watch.push.event.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomePagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    @BindView(R.id.header_parent)
    LinearLayout headerParent;

    @BindView(R.id.header_baby_avatar)
    ImageView headerBabyAvatar;

    @BindView(R.id.iv_battery)
    ImageView iv_battery;

    @BindView(R.id.header_title)
    TextView headerTitle;

    @BindView(R.id.header_baby_select)
    LinearLayout headerBabySelect;

    @BindView(R.id.header_baby_name)
    TextView headerBabyName;

    @BindView(R.id.header_qr_code)
    ImageView headerQrCode;

    @BindView(R.id.header_add)
    ImageView headerAdd;

    @BindView(R.id.view_pager)
    CustomViewPager view_pager;

    @BindView(R.id.tab_home)
    LinearLayout tab_home;

    @BindView(R.id.iv_home)
    ImageView iv_home;

    @BindView(R.id.tv_home)
    TextView tv_home;

    @BindView(R.id.tab_location)
    LinearLayout tab_location;

    @BindView(R.id.iv_location)
    ImageView iv_location;

    @BindView(R.id.tv_location)
    TextView tv_location;

    @BindView(R.id.tab_chat)
    RelativeLayout tab_chat;

    @BindView(R.id.iv_chat)
    ImageView iv_chat;

    @BindView(R.id.tv_chat)
    TextView tv_chat;

    @BindView(R.id.tab_mine)
    RelativeLayout tab_mine;

    @BindView(R.id.iv_mine)
    ImageView iv_mine;

    @BindView(R.id.tv_mine)
    TextView tv_mine;

    @BindView(R.id.iv_chat_has_new)
    ImageView iv_chat_has_new;

    @BindView(R.id.iv_mine_has_new)
    ImageView iv_mine_has_new;

    private CommonPopupWindow popupWindow;
    private CommonPopupWindow.LayoutGravity layoutGravity;

    private BabyAdapter babyAdapter;
    private RecyclerView babyRecycler;

    //相机权限
    private static final int REQ_CODE_PERMISSION = 0x01;

    private IHomeManager iHomeManager = HomeManager.getInstance();
    private IMineManager iMineManager = MineManager.getInstance();

    List<BindWatchBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_pager);
        ButterKnife.bind(this);
        setTranslucentStatus(true);
        AppManager.addActivity(this);
        EventBus.getDefault().register(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private void initView() {
        view_pager.setScanScroll(false);
        setViewSize(headerParent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        headerBabyAvatar.setVisibility(View.VISIBLE);
        headerBabySelect.setVisibility(View.VISIBLE);
        headerQrCode.setVisibility(View.VISIBLE);
        HomeFragment homeFragment = new HomeFragment();
        LocationFragment locationFragment = new LocationFragment();
        ChatFragment chatFragment = new ChatFragment();
        MineFragment mineFragment = new MineFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragmentDataList = new ArrayList<>();
        fragmentDataList.add(homeFragment);
        fragmentDataList.add(locationFragment);
        fragmentDataList.add(chatFragment);
        fragmentDataList.add(mineFragment);
        HomePagerAdapter viewPagerAdapter = new HomePagerAdapter(fragmentManager, fragmentDataList);
        view_pager.setAdapter(viewPagerAdapter);
        view_pager.setOffscreenPageLimit(4);
        view_pager.addOnPageChangeListener(this);
        selectFootTab(true, false, false, false);
        switchHeader(0);

        headerBabyAvatar.setOnClickListener(this);
        headerBabySelect.setOnClickListener(this);
        headerQrCode.setOnClickListener(this);
        headerAdd.setOnClickListener(this);
        tab_home.setOnClickListener(this);
        tab_location.setOnClickListener(this);
        tab_chat.setOnClickListener(this);
        tab_mine.setOnClickListener(this);
    }

    //从服务器拉取数据
    private void initData() {
        getBindWatchList();
        getCurrentUserInfo();
        getIntimateSet();
    }

    //获取绑定的手表列表
    private void getBindWatchList() {
        if (iHomeManager != null) {
            iHomeManager.listBindWatch(new BaseObserverListener<BaseResultBean<BindWatchBean[]>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(HomePagerActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<BindWatchBean[]> baseResultBean) {
                    mList.clear();
                    mList.addAll(Arrays.asList(baseResultBean.getData()));
                    babyAdapter = new BabyAdapter(HomePagerActivity.this, mList);
                    FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
                    View contentView = LayoutInflater.from(HomePagerActivity.this).inflate(R.layout.item_baby, decorView,false);
                    int withSpec = View.MeasureSpec.makeMeasureSpec(decorView.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
                    int heightSpec = View.MeasureSpec.makeMeasureSpec(decorView.getMeasuredHeight(), View.MeasureSpec.AT_MOST);
                    contentView.measure(withSpec, heightSpec);
                    popupWindow = new CommonPopupWindow(HomePagerActivity.this, R.layout.common_popup, ScreenAdapterUtil.getWidthPx(HomePagerActivity.this) / 2, contentView.getLayoutParams().height) {
                        @Override
                        protected void initView() {
                            View view = getContentView();
                            babyRecycler = view.findViewById(R.id.babyRecycler);
                        }

                        @Override
                        protected void initEvent() {
                            LinearLayoutManager layoutManager = new LinearLayoutManager(HomePagerActivity.this);
                            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                            babyRecycler.setLayoutManager(layoutManager);
                            babyRecycler.setAdapter(babyAdapter);
                            babyAdapter.setBabyClick(new BabyAdapter.BabyClick() {
                                @Override
                                public void onItem(View view, int position) {
                                    if (iHomeManager != null) {
                                        iHomeManager.switchWatch(mList.get(position).getWatchId(), new BaseObserverListener<BaseResultBean>() {
                                            @Override
                                            public void onCompleted() {
                                                popupWindow.getPopupWindow().dismiss();
                                            }
                                            @Override
                                            public void onError(Throwable e) {
                                            }

                                            @Override
                                            public void onNext(BaseResultBean baseResultBean) {
                                                if (baseResultBean.getCode() == 0) {
                                                    EventBus.getDefault().post(new AutoLoginEvent(1,null));
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    };
                    layoutGravity = new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.CENTER_HORIZONTAL | CommonPopupWindow.LayoutGravity.TO_BOTTOM);

                }
            });
        }
    }

    //获取当前用户信息和当前选中的手表信息
    private void getCurrentUserInfo() {
        if (iHomeManager != null) {
            iHomeManager.getCurrentUser(new BaseObserverListener<BaseResultBean<UserInfoBean>>() {
                @Override
                public void onCompleted() {
                    //通知MineFragment可以加载数据
                    UserInfoEvent userInfoEvent = new UserInfoEvent();
                    userInfoEvent.setLoad(true);
                    EventBus.getDefault().postSticky(userInfoEvent);
                    getCurrentWatchUserInfo();
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(HomePagerActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<UserInfoBean> baseResultBean) {
                    UserInfoBean.getInstance().setWatchUser(baseResultBean.getData().getWatchUser());
                    UserInfoBean.getInstance().setUser(baseResultBean.getData().getUser());
                    if(UserInfoBean.getInstance().getWatchUser().getHead().startsWith("sys")){
                        Glide.with(HomePagerActivity.this).load(PhotoUtils.getResource(UserInfoBean.getInstance().getWatchUser().getHead().substring(0,12))).into(headerBabyAvatar);
                    }else {
                        Glide.with(HomePagerActivity.this).load(UrlContants.FILE_URL + "resources/watch/" + UserInfoBean.getInstance().getWatchUser().getHead()).into(headerBabyAvatar);
                    }
                }
            });
        }
    }

    //获取当前手表用户信息
    private void getCurrentWatchUserInfo() {
        if (iHomeManager != null) {
            iHomeManager.getCurrentWatchUserInfo(new BaseObserverListener<BaseResultBean<WatchUserInfoBean>>() {
                @Override
                public void onCompleted() {
                    WatchUserInfoEvent event = new WatchUserInfoEvent();
                    event.setLoad(true);
                    EventBus.getDefault().post(event);
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(HomePagerActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<WatchUserInfoBean> baseResultBean) {
                    WatchUserInfoBean.getInstance().setWatchInfo(baseResultBean.getData().getWatchInfo());
                    WatchUserInfoBean.getInstance().setWatchUserInfo(baseResultBean.getData().getWatchUserInfo());
                    WatchUserInfoBean.getInstance().setWatchState(baseResultBean.getData().getWatchState());

                    boolean isOnline = WatchUserInfoBean.getInstance().getWatchState().getStatus().equals(1);
                    headerBabyName.setText(UserInfoBean.getInstance().getWatchUser().getName()+ (isOnline ? "(在线)" : "(离线)"));
                    if (WatchUserInfoBean.getInstance().getWatchState().getPower() != null) {
                        int battery = Integer.parseInt(WatchUserInfoBean.getInstance().getWatchState().getPower());
                        if (isOnline) {
                            if (battery > 80) {
                                iv_battery.setBackground(getResources().getDrawable(R.drawable.electricity_4));
                            } else if (battery > 60) {
                                iv_battery.setBackground(getResources().getDrawable(R.drawable.electricity_3));
                            } else if (battery > 40) {
                                iv_battery.setBackground(getResources().getDrawable(R.drawable.electricity_2));
                            } else if (battery > 20) {
                                iv_battery.setBackground(getResources().getDrawable(R.drawable.electricity_1));
                            } else if (battery > 0) {
                                iv_battery.setBackground(getResources().getDrawable(R.drawable.electricity_0));
                            }
                            iv_battery.setVisibility(View.VISIBLE);
                        } else {
                            iv_battery.setVisibility(View.GONE);
                        }
                    } else {
                        iv_battery.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    //获取贴心设置
    private void getIntimateSet() {
        if (iMineManager != null) {
            iMineManager.getIntimateSet(new BaseObserverListener<BaseResultBean<IntimateSetBean>>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(HomePagerActivity.this, getString(R.string.data_error));
                }

                @Override
                public void onNext(BaseResultBean<IntimateSetBean> baseResultBean) {
                    IntimateSetBean.getInstance().setAutoAnswer(baseResultBean.getData().isAutoAnswer());
                    IntimateSetBean.getInstance().setCallPosition(baseResultBean.getData().isCallPosition());
                    IntimateSetBean.getInstance().setPowerSaving(baseResultBean.getData().isPowerSaving());
                    IntimateSetBean.getInstance().setReservePower(baseResultBean.getData().isReservePower());
                    IntimateSetBean.getInstance().setShieldStrangeCall(baseResultBean.getData().isShieldStrangeCall());
                    IntimateSetBean.getInstance().setNoShutdown(baseResultBean.getData().isNoShutdown());
                    IntimateSetBean.getInstance().setEnableVolte(baseResultBean.getData().isEnableVolte());
                    IntimateSetBean.getInstance().setAutoShutdown(baseResultBean.getData().isAutoShutdown());
                    IntimateSetBean.getInstance().setAutoBoot(baseResultBean.getData().isAutoBoot());

                    IntimateSetBean.getInstance().setBootTime(baseResultBean.getData().getBootTime());
                    IntimateSetBean.getInstance().setShutdownTime(baseResultBean.getData().getShutdownTime());
                    IntimateSetBean.getInstance().setWatchId(baseResultBean.getData().getWatchId());
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_baby_avatar://宝贝头像
                startActivity(new Intent(this, WatchInfoActivity.class));
                break;
            case R.id.header_baby_select://切换宝贝手表
                if (popupWindow != null) {
                    popupWindow.showBashOfAnchor(headerParent, layoutGravity, 0, 0);
                }
                break;
            case R.id.header_qr_code://二维码扫描
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQ_CODE_PERMISSION);
                } else {
                    startCaptureActivityForResult();
                }
                break;
            case R.id.header_add:
                //添加微聊组
                startActivity(new Intent(this, SelectWatchActivity.class));
                break;
            case R.id.tab_home:
                selectFootTab(true, false, false, false);
                view_pager.setCurrentItem(0);
                break;
            case R.id.tab_location:
                selectFootTab(false, true, false, false);
                view_pager.setCurrentItem(1);
                break;
            case R.id.tab_chat:
                selectFootTab(false, false, true, false);
                view_pager.setCurrentItem(2);
                break;
            case R.id.tab_mine:
                selectFootTab(false, false, false, true);
                view_pager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    private void switchHeader(int viewPagerIndex) {
        switch (viewPagerIndex) {
            case 0:
                headerBabyAvatar.setVisibility(View.VISIBLE);
                headerTitle.setVisibility(View.GONE);
                headerBabySelect.setVisibility(View.VISIBLE);
                headerQrCode.setVisibility(View.VISIBLE);
                headerAdd.setVisibility(View.GONE);
                break;
            case 1:
                headerBabyAvatar.setVisibility(View.VISIBLE);
                headerTitle.setVisibility(View.VISIBLE);
                headerBabySelect.setVisibility(View.GONE);
                headerQrCode.setVisibility(View.GONE);
                headerAdd.setVisibility(View.GONE);
                headerTitle.setText(getString(R.string.tab_location));
                break;
            case 2:
                headerBabyAvatar.setVisibility(View.VISIBLE);
                headerTitle.setVisibility(View.VISIBLE);
                headerBabySelect.setVisibility(View.GONE);
                headerQrCode.setVisibility(View.GONE);
                headerAdd.setVisibility(View.VISIBLE);
                headerTitle.setText(getString(R.string.tab_chat));
                break;
            case 3:
                headerBabyAvatar.setVisibility(View.VISIBLE);
                headerTitle.setVisibility(View.VISIBLE);
                headerBabySelect.setVisibility(View.GONE);
                headerQrCode.setVisibility(View.GONE);
                headerAdd.setVisibility(View.GONE);
                headerTitle.setText(getString(R.string.tab_mine));
                break;
        }
    }

    private void selectFootTab(boolean home, boolean position, boolean message, boolean me) {
        if (home) {
            iv_home.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_true));
            tv_home.setTextColor(getResources().getColor(R.color.yellow_1));
            switchHeader(0);
        } else {
            iv_home.setImageDrawable(getResources().getDrawable(R.drawable.ic_home_false));
            tv_home.setTextColor(getResources().getColor(R.color.gray_1));
        }
        if (position) {
            iv_location.setImageDrawable(getResources().getDrawable(R.drawable.ic_position_true));
            tv_location.setTextColor(getResources().getColor(R.color.yellow_1));
            switchHeader(1);
        } else {
            iv_location.setImageDrawable(getResources().getDrawable(R.drawable.ic_position_false));
            tv_location.setTextColor(getResources().getColor(R.color.gray_1));
        }
        if (message) {
            iv_chat.setImageDrawable(getResources().getDrawable(R.drawable.ic_message_true));
            tv_chat.setTextColor(getResources().getColor(R.color.yellow_1));
            switchHeader(2);
        } else {
            iv_chat.setImageDrawable(getResources().getDrawable(R.drawable.ic_message_false));
            tv_chat.setTextColor(getResources().getColor(R.color.gray_1));
        }
        if (me) {
            iv_mine.setImageDrawable(getResources().getDrawable(R.drawable.ic_me_true));
            tv_mine.setTextColor(getResources().getColor(R.color.yellow_1));
            switchHeader(3);
        } else {
            iv_mine.setImageDrawable(getResources().getDrawable(R.drawable.ic_me_false));
            tv_mine.setTextColor(getResources().getColor(R.color.gray_1));
        }
    }

    private void startCaptureActivityForResult() {
        IntentIntegrator intentIntegrator = new IntentIntegrator(HomePagerActivity.this);
        intentIntegrator.setCaptureActivity(ScanQRCodeActivity.class);
        intentIntegrator.initiateScan();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQ_CODE_PERMISSION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startCaptureActivityForResult();
                } else {
                    ToastUtils.showLong(this, getString(R.string.have_to_agree_premissions));
                }
            }
            break;
        }
    }

    //拿到扫描返回数据，去绑定
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        // 获取解析结果
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() != null) {
                String content = result.getContents();
                if (iHomeManager != null) {
                    iHomeManager.checkImei(content, new BaseObserverListener<BaseResultBean<ImeiBean>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtils.showShort(HomePagerActivity.this, getString(R.string.data_error));
                        }

                        @Override
                        public void onNext(BaseResultBean<ImeiBean> baseResultBean) {
                            if (baseResultBean.getCode() == 0) {
                                if (baseResultBean.getData().getUserRole() == 0) {
                                    startActivity(new Intent(HomePagerActivity.this, BabyInfoActivity.class).putExtra("imei", content).putExtra("userRole", baseResultBean.getData().getUserRole()).putExtra("showRelation",true));
                                } else if (baseResultBean.getData().getUserRole() == 1) {
                                    startActivity(new Intent(HomePagerActivity.this,BabyRelationShipActivity.class).putExtra("imei", content));
                                }
                            } else {
                                ToastUtils.showShort(HomePagerActivity.this,baseResultBean.getMsg());
                            }
                        }
                    });
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                selectFootTab(true, false, false, false);
                break;
            case 1:
                selectFootTab(false, true, false, false);
                break;
            case 2:
                selectFootTab(false, false, true, false);
                break;
            case 3:
                selectFootTab(false, false, false, true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {
    }

    public void setViewSize(View view, int width, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    private CustomDialog mDialog;

    @Override
    public void onBackPressed() {
        mDialog = new CustomDialog(this, getString(R.string.warm_prompt), "确认退出APP?", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                AppManager.finishAllActivity();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        },getString(R.string.confirm),getString(R.string.cancel));
        mDialog.setCanotBackPress();
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
        }
    }

    //初始化/刷新 数据
    @Subscribe(sticky = true ,threadMode = ThreadMode.MAIN)
    public void loginStatus(AutoLoginEvent autoLoginEvent){
        if(autoLoginEvent.getAuto() == 1) {
            initData();
        }
    }

    //修改手表信息会刷新主页数据
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setAvatar(UpdateWatchInfoEvent event){
        getCurrentUserInfo();
        getBindWatchList();
    }

    //收到新消息推送
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasNewMsg(MessageEvent event) {
        if (event.getType() == MessageContants.CHAT) {
            iv_chat_has_new.setVisibility(View.VISIBLE);
        }
    }

    //某个群组消息全部已读
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasReadAllMsg(HasReadAllMsgEvent event) {
        if (event.getAllRead()) {
            iv_chat_has_new.setVisibility(View.GONE);
        }
    }

    //在拉取群组时如果发现未读消息(看见但是未读)或新消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void notReadMsg(NotReadMsgEvent event) {
        if (event.getType() == MessageContants.CHAT) {
            iv_chat_has_new.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        AppManager.finishActivity(this);
    }

}